﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;

namespace VietStar.Library.Commom.Algorithm
{
    public class Validators
    {
        public Validators()
        { }

        public bool IsImageFile(string filename)
        {
            string[] validExt = { "jpg", "jpeg", "png", "gif", "bmp" };
            if (!string.IsNullOrEmpty(filename))
            {
                string fileExt = filename.Substring(filename.LastIndexOf('.') + 1).ToLower();
                for (int i = 0; i < validExt.Length; i++)
                {
                    if (fileExt.Equals(validExt[i]))
                        return true;
                }
            }
            return false;
        }

        public bool IsEmail(string email)
        {
            email = email.Trim();
            Regex regEmail = new Regex(@"^[a-zA-Z0-9][\w\.-]*[a-zA-Z0-9]@[a-zA-Z0-9][\w\.-]*[a-zA-Z0-9]\.[a-zA-Z][a-zA-Z\.]*[a-zA-Z]$");
            return regEmail.IsMatch(email);
        }

        public bool IsNumeric(string values)
        {
            values = values.Trim();
            return (Regex.IsMatch(values, @"^[\+\-]?\d*\.?[Ee]?[\+\-]?\d*$"));
        }

        public bool IsDateTime(string values, bool mmddyyyy)
        {
            values = values.Trim();
            if (mmddyyyy)
                return (Regex.IsMatch(values, @"(0[1-9]|1[012])[- /.](0[1-9]|[12][0-9]|3[01])[- /.](19|20)[0-9]{2}"));
            else
                return (Regex.IsMatch(values, @"(0[1-9]|[12][0-9]|3[01])[- /.](0[1-9]|1[012])[- /.](19|20)[0-9]{2}"));
        }
    }
}
